package anim;

import dae.Animation;
import dae.Channel;
import dae.Input;
import dae.Sampler;
import dae.Source;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class AnimationClips
{
    public static final Logger logger = LoggerFactory.getLogger(AnimationClips.class);
    //此骨骼的
    public Map <String, AnimFragments> allAnimMap = new TreeMap <>();
    //可供查询
    public Map <Sampler, String> samplerTargetMap = new HashMap <>();
    private Animation sourceAnimData;
    /**
     * 内部数据
     */
    private Map <String, Source> sourceMap = new HashMap <>();
    private Map <String, Sampler> samplerMap = new HashMap <>();

    public AnimationClips(Animation sourceAnimData)
    {
        this.sourceAnimData = sourceAnimData;
        init();
    }

    public void init()
    {
        sourceAnimData.sampler.forEach(data -> samplerMap.put(data.id, data));
        sourceAnimData.source.forEach(data -> sourceMap.put(data.id, data));
        for (Channel channelData : sourceAnimData.channel)
        {
            String samplerId = channelData.getSource();//source="#"+samplerId;
            String target = channelData.getTarget();
            samplerTargetMap.put(samplerMap.get(samplerId), target);
        }
        //从每个sampler中取出数据，构造动画片段
        for (Sampler sampler : samplerMap.values())
        {
            String boneName = sampler.id.replace(ParseAnimUtil.SAMPLER_POSTFIX, "").replace(ParseAnimUtil.NODE_PREFIX, "");
            //时间轴
            Input inputInfo = ParseAnimUtil.getSpecialTypeFromInput(sampler, ParseAnimUtil.INPUT_TYPE);
            //数据
            Input outputInfo = ParseAnimUtil.getSpecialTypeFromInput(sampler, ParseAnimUtil.OUTPUT_TYPE);
            //插值方式
            Input interpolationInfo = ParseAnimUtil.getSpecialTypeFromInput(sampler, ParseAnimUtil.INTERPOLATION_TYPE);
            //一块骨骼的所有数据
            AnimFragments animFragments = new AnimFragments(inputInfo, outputInfo, interpolationInfo, sourceMap);
            allAnimMap.put(boneName, animFragments);
        }
    }

}
